#include "widget.h"
#include "ui_widget.h"


#include <QNetworkDatagram>
const QString& SERVER_IP = "127.0.0.1";
const quint16 SERVER_PORT = 9090;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    socket = new QUdpSocket(this);

    this->setWindowTitle("client");

      connect(socket, &QUdpSocket::readyRead, this, &Widget::processResponse);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    // 1. 获取到输入框的内容
       const QString& text = ui->lineEdit->text();
       // 2. 构造 UDP 的请求数据.
       QNetworkDatagram requestDatagram(text.toUtf8(), QHostAddress(SERVER_IP), SERVER_PORT);
       // 3. 发送请求数据.
       socket->writeDatagram(requestDatagram);
       // 4. 把发送的请求也添加到列表框中.
       ui->listWidget->addItem("client say : " + text);
       // 5. 把输入框的内容也清空一下.
       ui->lineEdit->setText("");
}

void Widget::processResponse()
{
    // 通过这个函数来处理收到的响应.
      // 1. 读取到响应数据
      const QNetworkDatagram& responseDatagram = socket->receiveDatagram();
      QString response = responseDatagram.data();
      //qDebug()<<response;
      // 2. 把响应数据显示到界面上.
      ui->listWidget->addItem("server say: " + response);
}
